package cn.training.controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.training.bean.UserBean;
import cn.training.service.HelloWorldService;

@Slf4j
@Controller("HelloWorldController")
@RequestMapping("/")
public class HelloWorldController {

	@Autowired
	HelloWorldService helloWorldService;
//跳转到程序的首页(index.html)	
   @RequestMapping(value = "/", method = RequestMethod.GET)
   public String index(Model model) {
	   UserBean userBean=new UserBean();
	   model.addAttribute("userBean", userBean);
       return "index";
   }
   

	 
//首页(index.html)的登录验证	 
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="searchall")
    public String initLoginall(Model model, HttpSession session, 
    		@Valid @ModelAttribute("userBean") UserBean userBean,
    		BindingResult results) throws SQLException{
    	
    	if (results.hasErrors()) {
			log.info("内容验证出错");
			return "index";
		}
    	
    	UserBean result1 = helloWorldService.searchUser(userBean);
    	
    	if( result1 != null)
        { 
    	 List<UserBean> result = helloWorldService.searchallUser(userBean);
       	 model.addAttribute("list", result);
       	 return "show";
        }
        else
        {model.addAttribute("indexmessage", "您输入的用户ID或密码错误，请重新输入！"); 
        
        }  	
    	 return "index";
  }
    
//首页(index.html)跳转到注册页面(zhuce.html)    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="zhuce")
    public String initZhuce(UserBean userBean,Model model) {
        return "zhuce";
    }
    
    
    
    
//注册页面(zhuce.html)的提交    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="submit")
    public String initSubmit(Model model, HttpSession session, 
    		@Valid @ModelAttribute("userBean") UserBean usersubmit,
    		BindingResult results) throws SQLException{
    	
    	if (results.hasErrors()) {
			log.info("内容验证出错");
			return "zhuce";
		}
    	
        String id=usersubmit.getUserId().toString();
        String password=usersubmit.getUserPassword().toString();
        String passwordconfirm=usersubmit.getPasswordConfirm().toString();
      // if("".equals(id)||"".equals(password))
        if(id.isEmpty()||password.isEmpty())
        {   model.addAttribute("userBean", usersubmit);
        	model.addAttribute("success", "用户名和密码不能为空，请重新填写！");
        	return "zhuce";
        }
        else if(!password.equals(passwordconfirm))
        {   model.addAttribute("userBean", usersubmit);
        	model.addAttribute("success", "两次密码输入不一致，请重新确认密码！");
    	   return "zhuce";
    	}
        else
        { int submit = helloWorldService.submit(usersubmit);
          model.addAttribute("success", "注册成功，请登录！");
          return "zhuce";}
    }
    
//注册页面(zhuce.html)返回到首页(index.html)登录     
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="back")
    public String initBack(UserBean userBean,Model model) {
        return "index";
    }
    
    
    
    
    
//列表页面(show.html)跳转到删除确认页面(confirm.html)    
    @RequestMapping(value = "/delete-new", method = RequestMethod.GET)
    public String initConfirm(UserBean userBean,Model model) {
        return "confirm";
    }
    
//列表页面(show.html)跳转到更新页面(update-new.html)    
    @RequestMapping(value = "/update-new", method = RequestMethod.GET)
    public String initUpdatenew(UserBean userBean,Model model) {
    	UserBean result = helloWorldService.searchupUser(userBean);
    	model.addAttribute("userBean", result);
        return "update-new";
    }
    
//列表页面(show.html)跳转到程序的首页(index.html)	 
  	 @RequestMapping(value = "/init", method = RequestMethod.POST,params="returnindex")
  	    public String returnindex(UserBean userBean,Model model) {
  		 return "index";
  	 }  
  	 
 /** //更新页面(update-new.html)的修改判断  
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="update")
    public String initUpdate(UserBean userupdate, Model model) { 
    	String updateps=userupdate.getUserPassword().toString();
        String updatepsc=userupdate.getPasswordConfirm().toString();
        if(updateps.equals(updatepsc))
        {int update = helloWorldService.update(userupdate);
    	 model.addAttribute("updatemessage", "修改成功！");}
        else
        {model.addAttribute("updatemessage", "请重新确认密码！");}
        return "update-new";
    }*/	 
  	 
  	 	 
  
//更新页面(update-new.html)的修改判断  ----应用validation
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="update")
    public String initUpdate(Model model, HttpSession session, 
    		@Valid @ModelAttribute("userBean") UserBean userupdate,
    		BindingResult results) throws SQLException{ 
    	if (results.hasErrors()) {
			log.info("内容验证出错");
			return "update-new";
		}
    		
    	String updateps=userupdate.getUserPassword().toString();
        String updatepsc=userupdate.getPasswordConfirm().toString();
        if(updateps.equals(updatepsc))
        {int update = helloWorldService.update(userupdate);
    	 model.addAttribute("updatemessage", "修改成功！");}
        else
        {model.addAttribute("updatemessage", "请重新确认密码！");}
        return "update-new";
    }
    
//更新页面(update-new.html)取消操作
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="cancel")
    public String initCancel(UserBean usercancel, Model model) {    	
    	return "update-new";
    }
    
//更新页面(update-new.html)跳转回列表页面(show.html)
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="return-show")
    public String initReturn(UserBean userreturn, Model model) {    	
    	List<UserBean> result = helloWorldService.searchallUser(userreturn);
      	 model.addAttribute("list", result);
      	 return "show";
    }
    
    
    
    
 //删除确认页面(confirm.html)的确认操作 
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="yes")
    public String initYes(UserBean userdelete,Model model) {
    	int delete = helloWorldService.delete(userdelete);
    	List<UserBean> result = helloWorldService.searchallUser(userdelete);
     	 model.addAttribute("list", result);
     	 model.addAttribute("yes", "删除成功！");
		return "show";
    }
    
//删除确认页面(confirm.html)的取消操作
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="no")
    public String initNo(UserBean userBean,Model model) {
    	List<UserBean> result = helloWorldService.searchallUser(userBean);
      	 model.addAttribute("list", result);
      	 model.addAttribute("yes", "取消删除！");
      	 return "show";
    }

    
    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="search")
    public String initLogin(UserBean userBean, Model model) {
    	
    	UserBean result = helloWorldService.searchUser(userBean);
    	model.addAttribute("userBean", result);
        return "login";
    }
    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="insert")
    public String initInsert(UserBean userinsert, Model model) {
    	
    	int insert = helloWorldService.insert(userinsert);
        return "login";
    }
      
}
